#include"Stack.h"

int IsFull(Stack* ps)
{
	return ps->top == -1;
}
void StackResize(Stack* ps)
{
	ps->capacity *= 2;
	ps->a = (STDataType*)malloc(sizeof(STDataType) * ps->capacity);
	if (ps->a == NULL)
	{
		printf("fail malloc\n");
		exit(-1);
	}
}

void StackInit(Stack* ps)
{
	ps->a = (STDataType*)malloc(sizeof(STDataType) * 10);
	if (ps->a == NULL)
	{
		printf("fail malloc\n");
		exit(-1);
	}
	ps->top = -1;
	ps->capacity = 10;
}

void StackPush(Stack* ps, STDataType data)
{
	if (StackEmpty(ps))
	{
		StackResize(ps);
	}
	ps->a[++ps->top] = data;
}

void StackPop(Stack* ps)
{
	if (IsFull(ps))
	{
		printf("\nStack is empty.\n");
		exit(-1);
	}
	printf("%d ", ps->a[ps->top]);
	ps->top--;
}

STDataType StackTop(Stack* ps)
{
	if (IsFull(ps))
	{
		printf("Stack is empty.\n");
		exit(-1);
	}
	return ps->a[ps->top];
}

int StackSize(Stack* ps)
{
	return ps->top + 1;
}

int StackEmpty(Stack* ps)
{
	return ps->top == ps->capacity - 1;
}

void StackDestroy(Stack* ps)
{
	free(ps->a);
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = -1;
}
